<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="112">
      <el-form-item label="比赛标题" prop="title">
        <el-input v-model="queryParams.title" placeholder="请输入比赛标题" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="比赛开始时间" style="width: 308px">
        <el-date-picker v-model="daterangeStartTime" value-format="YYYY-MM-DD" type="daterange" range-separator="-"
          start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item label="比赛结束时间" style="width: 308px">
        <el-date-picker v-model="daterangeEndTime" value-format="YYYY-MM-DD" type="daterange" range-separator="-"
          start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item label="报名开始时间" style="width: 308px">
        <el-date-picker v-model="daterangeSignStartTime" value-format="YYYY-MM-DD" type="daterange" range-separator="-"
          start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item label="报名结束时间" style="width: 308px">
        <el-date-picker v-model="daterangeSignEndTime" value-format="YYYY-MM-DD" type="daterange" range-separator="-"
          start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item label="浏览数量" prop="views">
        <el-input v-model="queryParams.views" placeholder="请输入浏览数量" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          v-hasPermi="['business:competition:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['business:competition:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['business:competition:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['business:competition:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="competitionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!--      <el-table-column label="主键" align="center" prop="competitionId" />-->
      <el-table-column label="比赛标题" align="center" prop="title" />
      <el-table-column label="比赛简介" align="center" prop="description" />
      <el-table-column label="封面" align="center" prop="cover" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.cover" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="比赛开始时间" align="center" prop="startTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="比赛结束时间" align="center" prop="endTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="报名开始时间" align="center" prop="signStartTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.signStartTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="报名结束时间" align="center" prop="signEndTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.signEndTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="浏览数量" align="center" prop="views" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="{ row }">
          <el-button link type="warning" icon="Management"
            @click="$router.push(`/competition/works/index/${row.competitionId}`)"
            v-hasPermi="['business:works:list']">作品管理</el-button>
          <el-button link type="primary" icon="Edit" @click="handleUpdate(row)"
            v-hasPermi="['business:competition:edit']">修改</el-button>
          <el-button link type="danger" icon="Delete" @click="handleDelete(row)"
            v-hasPermi="['business:competition:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改比赛对话框 -->
    <el-dialog :title="title" v-model="open" width="70%" append-to-body>
      <el-form ref="competitionRef" :model="form" :rules="rules" label-width="120">
        <el-form-item label="比赛标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="比赛简介" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="比赛开始时间" prop="startTime">
          <el-date-picker class="ff" clearable v-model="form.startTime" type="datetime" placeholder="请选择比赛开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="比赛结束时间" prop="endTime">
          <el-date-picker class="ff" clearable v-model="form.endTime" type="datetime" placeholder="请选择比赛结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="报名开始时间" prop="signStartTime">
          <el-date-picker class="ff" clearable v-model="form.signStartTime" type="datetime" placeholder="请选择报名开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="报名结束时间" prop="signEndTime">
          <el-date-picker class="ff" clearable v-model="form.signEndTime" type="datetime" placeholder="请选择报名结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="比赛评委" prop="judgeIds" v-if="!form.competitionId">
          <el-select-v2 class="ff" v-model="form.judgeIds" filterable :options="judges" placeholder="请选择评委"
            style="vertical-align: middle" multiple />
        </el-form-item>
        <el-form-item label="封面" prop="cover">
          <image-upload :limit="1" v-model="form.cover" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Competition">
import {
  listCompetition,
  getCompetition,
  delCompetition,
  addCompetition,
  updateCompetition,
  listJudge
} from "@/api/business/competition";
import { parseTime } from "@/utils/ruoyi";
import { getCurrentInstance, reactive, ref, toRefs } from "vue";

const { proxy } = getCurrentInstance();
const judges = ref([])
const competitionList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const daterangeStartTime = ref([]);
const daterangeEndTime = ref([]);
const daterangeSignStartTime = ref([]);
const daterangeSignEndTime = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    title: null,
    description: null,
    cover: null,
    startTime: null,
    endTime: null,
    signStartTime: null,
    signEndTime: null,
    views: null,
  },
  rules: {
    title: [
      { required: true, message: "比赛标题不能为空", trigger: "blur" }
    ],
    description: [
      { required: true, message: "比赛简介不能为空", trigger: "blur" }
    ],
    cover: [
      { required: true, message: "封面不能为空", trigger: "blur" }
    ],
    startTime: [
      { required: true, message: "比赛开始时间不能为空", trigger: "blur" }
    ],
    endTime: [
      { required: true, message: "比赛结束时间不能为空", trigger: "blur" }
    ],
    signStartTime: [
      { required: true, message: "报名开始时间不能为空", trigger: "blur" }
    ],
    signEndTime: [
      { required: true, message: "报名结束时间不能为空", trigger: "blur" }
    ],
    judgeIds: [
      { required: true, message: "请为本赛事指派评委", trigger: "blur" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);
/*评委*/
function listJudges() {
  listJudge().then(res => {
    judges.value = res.data
  })
}
/** 查询比赛列表 */
function getList() {
  loading.value = true;
  queryParams.value.params = {};
  if (null != daterangeStartTime && '' != daterangeStartTime) {
    queryParams.value.params["beginStartTime"] = daterangeStartTime.value[0];
    queryParams.value.params["endStartTime"] = daterangeStartTime.value[1];
  }
  if (null != daterangeEndTime && '' != daterangeEndTime) {
    queryParams.value.params["beginEndTime"] = daterangeEndTime.value[0];
    queryParams.value.params["endEndTime"] = daterangeEndTime.value[1];
  }
  if (null != daterangeSignStartTime && '' != daterangeSignStartTime) {
    queryParams.value.params["beginSignStartTime"] = daterangeSignStartTime.value[0];
    queryParams.value.params["endSignStartTime"] = daterangeSignStartTime.value[1];
  }
  if (null != daterangeSignEndTime && '' != daterangeSignEndTime) {
    queryParams.value.params["beginSignEndTime"] = daterangeSignEndTime.value[0];
    queryParams.value.params["endSignEndTime"] = daterangeSignEndTime.value[1];
  }
  listCompetition(queryParams.value).then(response => {
    competitionList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    competitionId: null,
    title: null,
    description: null,
    cover: null,
    startTime: null,
    endTime: null,
    signStartTime: null,
    signEndTime: null,
    views: null,
    delFlag: null,
    createTime: null,
    updateTime: null,
    createBy: null,
    updateBy: null,
    deptId: null,
    remark: null,
    judgeIds: []
  };
  proxy.resetForm("competitionRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  daterangeStartTime.value = [];
  daterangeEndTime.value = [];
  daterangeSignStartTime.value = [];
  daterangeSignEndTime.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.competitionId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加比赛";
  listJudges()
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _competitionId = row.competitionId || ids.value
  listJudges()
  getCompetition(_competitionId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改比赛";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["competitionRef"].validate(valid => {
    if (valid) {
      if (form.value.competitionId != null) {
        updateCompetition(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addCompetition(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _competitionIds = row.competitionId || ids.value;
  proxy.$modal.confirm('是否确认删除比赛编号为"' + _competitionIds + '"的数据项？').then(function () {
    return delCompetition(_competitionIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('business/competition/export', {
    ...queryParams.value
  }, `competition_${new Date().getTime()}.xlsx`)
}

getList();
</script>
